package com.xmtlxx.web.controller.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.web.controller.wms.service.IPutDtlService;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.wms.domain.bo.PutDtlBo;
import com.xmtlxx.wms.domain.vo.AsynInfo;
import com.xmtlxx.wms.domain.vo.PutDtlVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 上架任务明细Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "上架任务明细控制器", tags = {"上架任务明细管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/putDtl")
public class PutDtlController extends BaseController {

    private final IPutDtlService iPutDtlService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<PutDtlVo> initBean(){
        PutDtlVo vo = new PutDtlVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询上架任务明细列表
     */
    @ApiOperation("查询上架任务明细列表")
    @SaCheckPermission("wms:putDtl:list")
    @GetMapping("/list")
    public TableDataInfo<PutDtlVo> list(@Validated(QueryGroup.class) PutDtlBo bo, PageQuery pageQuery) {
        return iPutDtlService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出上架任务明细列表
     */
    @ApiOperation("导出上架任务明细列表")
    @SaCheckPermission("wms:putDtl:export")
    @Log(title = "上架任务明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated PutDtlBo bo, HttpServletResponse response) {
        List<PutDtlVo> list = iPutDtlService.queryList(bo);
        ExcelUtil.exportExcel(list, "上架任务明细", PutDtlVo.class, response);
    }

    /**
     * 获取上架任务明细详细信息
     */
    @ApiOperation("获取上架任务明细详细信息")
    @SaCheckPermission("wms:putDtl:query")
    @GetMapping("/{id}")
    public R<PutDtlVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iPutDtlService.queryById(id));
    }

    /**
     * 新增上架任务明细
     */
    @ApiOperation("新增上架任务明细")
    @SaCheckPermission("wms:putDtl:add")
    @Log(title = "上架任务明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PutDtlVo vo) {
        PutDtlBo bo = new PutDtlBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iPutDtlService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改上架任务明细
     */
    @ApiOperation("修改上架任务明细")
    @SaCheckPermission("wms:putDtl:edit")
    @Log(title = "上架任务明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<PutDtlVo> edit(@Validated(EditGroup.class) @RequestBody PutDtlVo vo) {
        PutDtlBo bo = new PutDtlBo();
        BeanUtil.copyProperties(vo,bo);

        PutDtlVo putDtlVo = iPutDtlService.updateByBo(bo);
        if(putDtlVo != null && !StringUtils.isEmpty(vo.getUuid())){
            AsynInfo.updateAsynInfoItem(vo.getUuid(),iPutDtlService.queryById(vo.getId()));
        }
        return R.ok(putDtlVo);
    }


    /**
     * 修改上架任务明细
     */
    @ApiOperation("修改上架任务明细")
    @SaCheckPermission("wms:putDtl:edit")
    @Log(title = "上架任务明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/editDtl")
    @Transactional(rollbackFor = Exception.class)
    public R<PutDtlVo> editDtl(@Validated(EditGroup.class) @RequestBody PutDtlVo vo) {
        PutDtlBo bo = new PutDtlBo();
        BeanUtil.copyProperties(vo,bo);

        PutDtlVo putDtlVo = iPutDtlService.updateByLocatorId(bo);
        return R.ok(putDtlVo);
    }


    /**
     * 修改上架任务明细
     */
    @ApiOperation("修改上架任务明细")
    @SaCheckPermission("wms:putDtl:edit")
    @Log(title = "上架任务明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/box")
    @Transactional(rollbackFor = Exception.class)
    public R<PutDtlVo> editBox(@Validated(EditGroup.class) @RequestBody PutDtlVo vo) {
        if(vo != null && vo.getItems() != null && vo.getItems().size() > 0){
            List<PutDtlVo> newItems = new ArrayList<>();
            for(PutDtlVo item : vo.getItems()){
                PutDtlBo bo = new PutDtlBo();
                BeanUtil.copyProperties(item,bo);
                bo.setBoxNo(vo.getBoxNo());
                bo.setLocatorId(vo.getLocatorId());
                PutDtlVo putDtlVo = iPutDtlService.updateByBo(bo);
                newItems.add(putDtlVo);
                if(putDtlVo != null && !StringUtils.isEmpty(vo.getUuid())){
                    AsynInfo.updateAsynInfoItem(vo.getUuid(),iPutDtlService.queryById(vo.getId()));
                }

            }
            vo.setItems(newItems);
        }

        return R.ok(vo);
    }


    /**
     * 修改上架任务明细
     */
    @ApiOperation("修改上架任务明细")
    @SaCheckPermission("wms:putDtl:edit")
    @Log(title = "单个上架明细修改", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/editOne")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> editOne(@Validated(EditGroup.class) @RequestBody PutDtlVo vo) {
        PutDtlBo bo = new PutDtlBo();
        BeanUtil.copyProperties(vo,bo);

        return toAjax(iPutDtlService.updateByPutTak(bo));
    }

    /**
     * 修改上架任务明细
     */
    @ApiOperation("计算打印次数")
    @SaCheckPermission("wms:putDtl:edit")
    @Log(title = "单个上架明细修改", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/countPrints")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> countPrints(@Validated(EditGroup.class) @RequestBody PutDtlVo vo) {
        PutDtlBo bo = new PutDtlBo();
        BeanUtil.copyProperties(vo,bo);

        return toAjax(iPutDtlService.countByPrintFlag(bo));
    }
    /**
     * 删除上架任务明细
     */
    @ApiOperation("删除上架任务明细")
    @SaCheckPermission("wms:putDtl:remove")
    @Log(title = "上架任务明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iPutDtlService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
